package org.dubnation.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dubnation.base.annotation.DubnationPermission;
import org.dubnation.base.query.BaseQuery;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.dubnation.org.domain.Department;
import org.dubnation.org.service.impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@DubnationPermission(name = "部门管理")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    //增/改
    /**
     * post请求和put请求接收的参数如果是对象，必须要使用注解@RequestBody后端才能取到前端传过来的值
     * @param department
     * @return
     */
    @DubnationPermission(name = "新增/修改部门")
    @PutMapping
    @ApiOperation(value = "部门新增/修改")@ApiParam(name="部门",required = true)
    public AjaxResult insertOrUpdate(@RequestBody  Department department){
        try {
            if (Objects.isNull(department.getId())){
                //新增
                departmentService.insert(department);
                return AjaxResult.success();
            }else {
                //修改
                departmentService.update(department);
                return AjaxResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //删
    @DubnationPermission(name = "通过id删除部门")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //查所有
    @DubnationPermission(name = "查询所有部门")
    @GetMapping
    @ApiOperation(value = "查询部门所有数据")
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.queryAll();
            return AjaxResult.success(departments); //将查询到的所有数据返回到前端
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //查单个
    @DubnationPermission(name = "根据id查询部门")
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门")
    public AjaxResult getById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.queryById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //分页查询
    @DubnationPermission(name = "分页查询部门")
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult queryPage(@RequestBody BaseQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //批量删除
    @DubnationPermission(name = "批量删除部门")
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }

    //查询父部门
    @DubnationPermission(name = "查询父级部门")
    @GetMapping("/parent")
    @ApiOperation(value = "查询父级部门")
    public AjaxResult getParents(){
        try {
            List<Department> parents =  departmentService.getParents();
            return AjaxResult.success(parents);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败，请联系后台管理员");
        }
    }
}
